package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Properties;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;

public class Mhrmworkschedule extends X_hrm_workschedule {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5313770141376459678L;
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmworkschedule.class);

	public Mhrmworkschedule(Properties ctx, int hrm_workschedule_ID, String trxName) {
		super(ctx, hrm_workschedule_ID, trxName);
	}

	public Mhrmworkschedule(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
	}
	
	@Override
	public boolean delete(boolean force) {
		
		// Entries in this table cannot be removed
		throw new AdempiereException(Msg.translate(Env.getCtx(), "AccessCannotDelete"));
		
	}

	/**
public static Mhrmworkschedule getWorkshedule(int hrm_onedaytype_id, Timestamp dt){
		
		if(hrm_onedaytype_id == 0)
    		return null;
		
		Mhrmworkschedule hrmws = new Mhrmworkschedule(Env.getCtx(), 0, null);
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		// Employee data from the card
		String sql_ = "SELECT *	FROM HRM_WORKSCHEDULE \n" +
						"WHERE HRM_HOLIDAY != 'Y' \n" +
						"AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n" +
						"AND HRM_ONEDAYTYPE_ID = ? " +
						"AND TO_DATE('01-'||TO_CHAR(HRM_DATEFROM,'MM-YYYY'),'DD-MM-YYYY') <= ?	" +
						"AND TO_DATE('31-'||TO_CHAR(HRM_DATEFROM,'MM-YYYY'),'DD-MM-YYYY') >= ?	";
		try
		{
			pstmt = DB.prepareStatement(sql_,null);
			pstmt.setInt(1, Env.getAD_Client_ID(Env.getCtx()));
			pstmt.setInt(2, hrm_onedaytype_id);
			pstmt.setTimestamp(3, dt);
			pstmt.setTimestamp(4, dt);
			rs = pstmt.executeQuery();
			if(rs.next())
				hrmws = new Mhrmworkschedule(Env.getCtx(), rs, null);			
		}
		catch (SQLException e)
		{
			s_log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}

		return hrmws;
	}
*/
public static int getOneDayType (int hrm_shedulerule_id, Timestamp dt){
	
	if(hrm_shedulerule_id == 0)
		return 0;
	
	int result = 0;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	
	// Employee data from the card
	String sql_ = "SELECT HRM_ONEDAYTYPE_ID	FROM HRM_WORKSCHEDULE \n" +
					"WHERE HRM_HOLIDAY != 'Y' \n" +
					"AND AD_CLIENT_ID="       + Env.getAD_Client_ID(Env.getCtx()) + " \n" +
					"AND HRM_SHEDULERULE_ID=" + hrm_shedulerule_id + " \n" +
					"AND HRM_DATEFROM ="      + DB.TO_DATE(dt)     + " \n" ;
	try
	{
		pstmt = DB.prepareStatement(sql_,null);
		rs = pstmt.executeQuery();
		if(rs.next())
			return rs.getInt(1);			
	}
	catch (SQLException e)
	{
		s_log.log(Level.SEVERE, "product", e);
	}
	finally
	{
		DB.close(rs, pstmt);
		rs = null; pstmt = null;
	}

	return result;
 }

}
